package com.hue.concurrent.day04;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/*
    锁超时:
        tryLock()
 */
public class Demo09 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            System.out.println("t1尝试获取锁");

            //直接返回获取锁的结果
//            if (!lock.tryLock()) {
//                System.out.println("t1获取锁失败");
//                return;
//            }

            //设置等待一定时间
            try {
                if (!lock.tryLock(1, TimeUnit.SECONDS)) {
                    System.out.println("t1获取锁失败");
                    return;
                }
                System.out.println("t1获取锁成功");
                System.out.println("做事...");
                lock.unlock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        lock.lock();
        t1.start();
    }
}
